package com.bite.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.jwt.Claims;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bite.common.core.constants.Constants;
import com.bite.common.core.domain.R;
import com.bite.common.core.domain.TableDataInfo;
import com.bite.common.core.enums.ExamListType;
import com.bite.common.core.enums.ResultCode;
import com.bite.common.core.utils.JwtUtils;
import com.bite.common.core.utils.ThreadLocalUtil;
import com.bite.common.security.exception.ServiceException;
import com.bite.common.security.service.TokenService;
import com.bite.friend.domain.exam.Exam;
import com.bite.friend.domain.exam.dto.ExamQueryDTO;
import com.bite.friend.domain.exam.vo.ExamVO;
import com.bite.friend.domain.user.UserExam;
import com.bite.friend.manager.ExamCacheManager;
import com.bite.friend.mapper.exam.ExamMapper;
import com.bite.friend.mapper.user.UserExamMapper;
import com.bite.friend.service.exam.IUserExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserExamService implements IUserExamService {
    @Resource
    private ExamMapper examMapper;
    @Resource
    private UserExamMapper userExamMapper;
    @Resource
    private TokenService tokenService;
    @Resource
    private ExamCacheManager examCacheManager;

    @Value("${jwt.secret}")
    private String secret;
    @Override
    public int enter(String token, Long examId) {

        Exam exam = examMapper.selectById(examId);
        if(exam == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        if(exam.getStartTime().isBefore(LocalDateTime.now())) {
            //开始时间在当前时间之前  说明已开赛不能报名
            throw new ServiceException(ResultCode.EXAM_STARTED);
        }

        //查看用户是否已经报过名
        //Long userId = tokenService.getUserId(token,secret);
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        UserExam userExam = userExamMapper.selectOne(new LambdaQueryWrapper<UserExam>()
                .eq(UserExam::getExamId, examId)
                .eq(UserExam::getUserId, userId));
        if(userExam != null) {
            throw new ServiceException(ResultCode.USER_EXAM_HAS_ENTER);
        }
        examCacheManager.addUserExamCache(userId,examId);
        userExam = new UserExam();
        userExam.setExamId(examId);
        userExam.setUserId(userId);
        return userExamMapper.insert(userExam) ;
    }


        //先查询缓存（u:e:l:用户id）  如果缓存能够查询到
        //如果查询不到   数据库当中再去查询  并且将数据库中的数据同步给redis
        @Override
        public TableDataInfo list(ExamQueryDTO examQueryDTO) {
            Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
            examQueryDTO.setType(ExamListType.USER_EXAM_LIST.getValue());
            Long total = examCacheManager.getListSize(ExamListType.USER_EXAM_LIST.getValue(), userId);
            List<ExamVO> examVOList;
            if (total == null || total <= 0) {
                //从数据库中查询我的竞赛列表
                PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
                examVOList = userExamMapper.selectUserExamList(userId);
                examCacheManager.refreshCache(ExamListType.USER_EXAM_LIST.getValue(), userId);
                total = new PageInfo<>(examVOList).getTotal();
            } else {
                examVOList = examCacheManager.getExamVOList(examQueryDTO, userId);
                total = examCacheManager.getListSize(examQueryDTO.getType(), userId);
            }
            if (CollectionUtil.isEmpty(examVOList)) {
                return TableDataInfo.empty();
            }
            return TableDataInfo.success(examVOList, total);
        }

}
